package cz.cvut.fel.gfupan.gui.action;

import cz.cvut.fel.gfupan.gui.Session;
import cz.cvut.fel.gfupan.gui.util.Bundle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class NewAction implements ActionListener {

    private JDialog root;

    public NewAction(JDialog root) {
        this.root = root;
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if (Session.getInstance().isChanged()) {
            int n = JOptionPane.showConfirmDialog(root,
                    Bundle.get("Window.saveChanges"),
                    Bundle.get("Window.title"),
                    JOptionPane.YES_NO_CANCEL_OPTION);
            if (n == JOptionPane.YES_OPTION) {
                JFileChooser fc = new JFileChooser();
                int returnVal = fc.showSaveDialog(root);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    //save file
                    Session.getInstance().newMeasure();
                }
            } else if (n == JOptionPane.NO_OPTION) {
                Session.getInstance().newMeasure();
            }
        } else {
            if (root != null) {
                root.dispose();
            }
            Session.getInstance().newMeasure();
        }
    }
}
